package cn.stylefeng.guns.modular.food.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;

import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.system.model.Foodtype;
import cn.stylefeng.guns.modular.system.model.Medicaltype;
import cn.stylefeng.guns.modular.food.service.IFoodtypeService;

/**
 * 美食期刊控制器
 *
 * @author fengshuonan
 * @Date 2020-06-21 00:25:48
 */
@Controller
@RequestMapping("/foodtype")
public class FoodtypeController extends BaseController {

    private String PREFIX = "/food/foodtype/";

    @Autowired
    private IFoodtypeService foodtypeService;

    /**
     * 跳转到美食期刊首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "foodtype.html";
    }

    /**
     * 跳转到添加美食期刊
     */
    @RequestMapping("/foodtype_add")
    public String foodtypeAdd() {
        return PREFIX + "foodtype_add.html";
    }

    /**
     * 跳转到修改美食期刊
     */
    @RequestMapping("/foodtype_update/{foodtypeId}")
    public String foodtypeUpdate(@PathVariable Integer foodtypeId, Model model) {
        Foodtype foodtype = foodtypeService.selectById(foodtypeId);
        model.addAttribute("item",foodtype);
        LogObjectHolder.me().set(foodtype);
        return PREFIX + "foodtype_edit.html";
    }

    /**
     * 获取美食期刊列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	EntityWrapper<Foodtype> wrapper = new EntityWrapper<Foodtype>();
    	wrapper.like("goods_name", condition).or()
    	.like("id", condition);
        return foodtypeService.selectList(wrapper);
    }

    /**
     * 新增美食期刊
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Foodtype foodtype) {
        foodtypeService.insert(foodtype);
        return SUCCESS_TIP;
    }

    /**
     * 删除美食期刊
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer foodtypeId) {
        foodtypeService.deleteById(foodtypeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改美食期刊
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Foodtype foodtype) {
        foodtypeService.updateById(foodtype);
        return SUCCESS_TIP;
    }

    /**
     * 美食期刊详情
     */
    @RequestMapping(value = "/detail/{foodtypeId}")
    @ResponseBody
    public Object detail(@PathVariable("foodtypeId") Integer foodtypeId) {
        return foodtypeService.selectById(foodtypeId);
    }
    
    @InitBinder
   	public void initBinder(WebDataBinder binder) {
   	    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   	    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));        
   	}
}
